package cn.caplike.demo.repository.design.pattern.eventbus.jdk.extend;

import java.util.concurrent.Executors;

public class Client {

    public static void main(String[] args) {

        AsyncDispatcher dispatcher = new AsyncDispatcher(
                Executors.newSingleThreadExecutor()
        );

        // 注册处理器
        dispatcher.register(BaseEventType.HELLO, (BaseEvent e) -> {
            System.out.println("Hello " + e.getWord());
        });
        dispatcher.register(BaseEventType.HI, (BaseEvent e) -> {
            System.out.println("Hi " + e.getWord());
        });
        dispatcher.register(BaseEventType.class, (BaseEvent e) -> {
            System.out.println("通过 class 注册 " + e.getWord());
        });

        // 启动事件循环
        dispatcher.serviceStart();

        // 发送事件
        for (int i = 0; i < 10; i++) {
            dispatcher.dispatchEvent(new BaseEvent(BaseEventType.HELLO, "World"));
            dispatcher.dispatchEvent(new BaseEvent(BaseEventType.HI, "世界"));
        }
    }

    public enum BaseEventType {
        /** 测试事件类型 */
        HELLO,
        /** 测试事件类型 */
        HI,
    }

    public static class BaseEvent extends AbstractEvent<BaseEventType> {

        private final String word;

        public BaseEvent(final BaseEventType type, final String word) {
            super(type);
            this.word = word;
        }

        public String getWord() {
            return word;
        }

    }
}
